var testPageHTML;

function TestRunner() {
	this.tests = TestRunner.arguments;			
	this.testStore = new Array;
	this.testPageHTML = new TestPageHTML();
}

TestRunner.prototype.Init = 
function() {
	this.testPageHTML.setExceptions(0);
	this.testPageHTML.setFails(0);
	this.testPageHTML.setPasses(0);
	this.testPageHTML.setTotalTestCases(0);
	this.testPageHTML.setCompletedTestCases(0);
	this.runSuiteOfTestCases(this, true, null);
}

TestRunner.prototype.runSuiteOfTestCases = 
function(suiteOfTests, incrementTotalCount, testOutputList) {
	
	for (var noOfTest = 0; noOfTest < suiteOfTests.tests.length; noOfTest++) {
	
		if ( suiteOfTests.tests[noOfTest] instanceof TestCase ) 
			this.runASingleTest(suiteOfTests.tests[noOfTest], testOutputList, incrementTotalCount);
		
		if ( suiteOfTests.tests[noOfTest] instanceof GroupTest ) 
			this.runAGroupOfTests(suiteOfTests.tests[noOfTest], suiteOfTests.tests[noOfTest].name,  testOutputList, incrementTotalCount);			
	}
}


TestRunner.prototype.runASingleTest =
function(test, testOutputList, incrementTotalCount) {
	this.addTestToFinishedTests(test);
	
	if ( incrementTotalCount == true )
		this.testPageHTML.setTotalNoOfTests( this.testPageHTML.getTotalNoOfTests() + 1 );
	
	this.testPageHTML.addNewTestHTML(test.noOfTest, test.name, testOutputList);			
	this.runCase(test, incrementTotalCount);
}

TestRunner.prototype.runAGroupOfTests =
function(groupOfTests, groupName, testOutputList, incrementTotalCount) {

	var GroupTestList = this.testPageHTML.addNewTestGroupHTML(groupName, testOutputList);			
	for(var testInSuite = 0; testInSuite < groupOfTests.tests.length; testInSuite++)
		this.runASingleTest(groupOfTests.tests[testInSuite], GroupTestList, incrementTotalCount);
} 

TestRunner.prototype.runCase =
function(test, incrementCompletedCount) {
	this.testPageHTML.setTestStatusToLoading(test.noOfTest);
	
	var outerClassObject = this;
	
	test.request.onreadystatechange = function() {
		
		//
		
		if (test.request.readyState == 4) {
			if (test.request.status == 200)	{
				
				if ( incrementCompletedCount == true )
					outerClassObject.testPageHTML.setCompletedTestCases( outerClassObject.testPageHTML.getCompletedTestCases() + 1 );
				
				try	{						
					var response = test.request.responseXML.getElementsByTagName('testcase').item(0);
					
					outerClassObject.testPageHTML.updateTestResuts(test, response.getElementsByTagName('passes').item(0).firstChild.data, getNoOf('fails'), getNoOf('exceptions'), response.getElementsByTagName('msg'));
						
					refreshTestResultBG(getNoOf('fails'), getNoOf('exceptions'));
						
				}catch(exception) {
					alert('Error with XML or something: ' + exception + '; ResponseXML: ' + test.request.responseXML);
				}
				
			}else {
				alert('There was a problem with the request for `' + test.name + '`.');
			}
			
			outerClassObject.testPageHTML.setTestStatusToCompleted(test.noOfTest);
		}
	}

	test.request.open('GET', ROOT_TO_TESTGROUPER + NAME_OF_TESTGROUPER + '?filename=' + test.filename, true);
	test.request.send(null);
}
	

TestRunner.prototype.refreshCase = 
function(noOfTest){	
	if ( this.testIsDownloading(noOfTest) ) {
		alert('This test is already being downloaded.');
	} else {
		this.testPageHTML.removeTestResultMessage(noOfTest);		
		this.runCase(this.getFinishedTest(noOfTest));
	}
}

TestRunner.prototype.testIsDownloading =
function(noOfTest) {
	if ( this.testPageHTML.getTestStatus(noOfTest) == STATUS_LOADING )
		return TRUE;
}

TestRunner.prototype.addTestToFinishedTests = 
function(test) {
	test.noOfTest = this.testStore.length;
	this.testStore[this.testStore.length] = test;
}

TestRunner.prototype.getFinishedTest = 
function(noOfTest) {
	return this.testStore[noOfTest];
}

